package msu.ml.data;

import msu.ml.core.*;

public class TargetDataProxy
{
	private DataCache cache;
	private NxInstances unprocessed;
	private String [] files;

	public TargetDataProxy(DataCache cache, String [] files)
	{
		this.cache = cache;
		this.files = new String[files.length];
		System.arraycopy(files, 0, this.files, 0, files.length);
	}

	public int count()
	{
		return this.files.length;
	}

	public NxInstances get(int index)
	{
		unprocessed = null;
		unprocessed = getUnprocessed(index);

		NxInstances result = unprocessed;
		
		for(IDataPreProcessor processor : cache.getDataPreProcessors())
			if(!(processor instanceof SubsamplePreProcessor))
					result = processor.process(result); 

		return result;
	}

	public NxInstances getUnprocessed(int index)
	{
		if(unprocessed != null)
			return unprocessed;

		boolean cacheToDisk = cache.getCacheToDisk();
		cache.setCacheToDisk(false);

		NxInstances result = cache.retrieveUnprocessed(files[index]);
		
		cache.setCacheToDisk(cacheToDisk);
		return result;
	}

}
